package com.example.examsystem.controller;

import com.example.examsystem.dto.ExamDto;
import com.example.examsystem.dto.PageResponse;
import com.example.examsystem.model.ExamStatus;
import com.example.examsystem.service.ExamService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 考试控制器
 * 处理考试的CRUD操作和状态管理
 */
@RestController
@RequestMapping("/exams")
@RequiredArgsConstructor
public class ExamController {
    
    private final ExamService examService;
    
    /**
     * 获取考试列表
     * @param page 页码
     * @param size 每页大小
     * @param query 搜索关键词
     * @param status 考试状态
     * @param subjectId 学科ID
     * @return 分页考试列表
     */
    @GetMapping
    @PreAuthorize("hasAuthority('exam:list:view')")
    public ResponseEntity<PageResponse<ExamDto>> getExams(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String query,
            @RequestParam(required = false) ExamStatus status,
            @RequestParam(required = false) Long subjectId) {
        return ResponseEntity.ok(examService.getExams(page, size, query, status, subjectId));
    }
    
    /**
     * 获取单个考试详情
     * @param id 考试ID
     * @return 考试详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('exam:list:view')")
    public ResponseEntity<ExamDto> getExam(@PathVariable Long id) {
        return ResponseEntity.ok(examService.getExam(id));
    }
    
    /**
     * 创建新考试
     * @param examDto 考试信息
     * @return 创建成功的考试
     */
    @PostMapping
    @PreAuthorize("hasAuthority('exam:list:view')")
    public ResponseEntity<ExamDto> createExam(@Valid @RequestBody ExamDto examDto) {
        return ResponseEntity.ok(examService.createExam(examDto));
    }
    
    /**
     * 更新考试
     * @param id 考试ID
     * @param examDto 更新的考试信息
     * @return 更新后的考试
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('exam:list:edit')")
    public ResponseEntity<ExamDto> updateExam(
            @PathVariable Long id, 
            @Valid @RequestBody ExamDto examDto) {
        return ResponseEntity.ok(examService.updateExam(id, examDto));
    }
    
    /**
     * 删除考试
     * @param id 考试ID
     * @return 删除成功响应
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('exam:list:delete')")
    public ResponseEntity<Void> deleteExam(@PathVariable Long id) {
        examService.deleteExam(id);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 发布考试
     * @param id 考试ID
     * @return 发布后的考试
     */
    @PostMapping("/{id}/publish")
    @PreAuthorize("hasAuthority('exam:list:publish')")
    public ResponseEntity<ExamDto> publishExam(@PathVariable Long id) {
        return ResponseEntity.ok(examService.publishExam(id));
    }
    
    /**
     * 归档考试
     * @param id 考试ID
     * @return 归档后的考试
     */
    @PostMapping("/{id}/archive")
    @PreAuthorize("hasAuthority('exam:list:edit')")
    public ResponseEntity<ExamDto> archiveExam(@PathVariable Long id) {
        return ResponseEntity.ok(examService.archiveExam(id));
    }
}